package com.fanfutian.eventproject.controller;

import com.fanfutian.eventproject.pojo.Category;
import com.fanfutian.eventproject.pojo.Result;
import com.fanfutian.eventproject.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;


    /**
     * 当前用户新增分类
     * @return
     */
    @PostMapping
    public Result addCategory(@RequestBody @Validated(Category.Add.class) Category category) {
        log.info("添加文章分类··········{}",category.getCategoryName());
        categoryService.addCategory(category);
        return Result.success();
    }


    /**
     * 获取当前用户的文章分类列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> getCategoryList() {
        log.info("获取当前用户的文章分类列表··········");
        List<Category> pageBeanCategory = categoryService.getCategoryList();
        return Result.success(pageBeanCategory);
    }


    /**
     * 获取分类详情
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        log.info("获取分类详情··········用户id：{}",id);
        Category category = categoryService.detail(id);
        return Result.success(category);
    }


    /**
     * 更新文章分类
     * @return
     */
    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category){
        log.info("更新文章分类信息···········");
        categoryService.updateCategoryInfo(category);
        return Result.success();
    }


    /**
     * 删除文章分类
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCategory")
    public Result deleteCategory(Integer id){
        log.info("删除ID为 {} 的文章分类信息···········",id);
        categoryService.deleteCategoryInfo(id);
        return Result.success();
    }
}
